package com.wish.wishsystem_management.service.impl;

import com.wish.wishsystem_management.entity.SysUser;
import com.wish.wishsystem_management.mapper.SysUserMapper;
import com.wish.wishsystem_management.service.ISysUserRoleService;
import com.wish.wishsystem_management.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.wishsystem_management.service.ITbConfigUserBusinessCircleService;
import dto.systemDto.SysUserDto;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author zhaiwj
 * @since 2021-03-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ITbConfigUserBusinessCircleService iTbConfigUserBusinessCircleService;
    @Autowired
    private ISysUserRoleService iSysUserRoleService;


    @Override
    public List<SysUser> selectPageVo(SysUserDto sysUserDto) {
        return baseMapper.selectPageVo(sysUserDto);
    }


    @Override
    public SysUser queryObject(Long userId) {
        return baseMapper.queryObject(userId);
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        baseMapper.deleteBatch(userIds);

        iSysUserRoleService.deleteBatch(userIds);

        iTbConfigUserBusinessCircleService.deleteBatch(userIds);

    }

    @Override
    public SysUser getSysUserByMobile(String mobile) {
        return baseMapper.getSysUserByMobile(mobile);
    }

    @Override
    public List<SysUser> queryList(Map<String, Object> map) {
        List<SysUser> queryList = baseMapper.queryList(map);
        for (int i = 0; i < queryList.size(); i++) {
            Date creatTime = queryList.get(i).getCreateTime();
            //DateFormat dFormatPay = new SimpleDateFormat("yyyy-MM-dd");
            // String format = dFormatPay.format(creatTime);
            queryList.get(i).setCreateTime(creatTime);
        }
        return queryList;
    }

    @Override
    public void update(SysUser sysUser) {
        if (StringUtils.isBlank(sysUser.getPassword())) {
            sysUser.setPassword(null);
        } else {
            sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), sysUser.getSalt()).toHex());
        }
        baseMapper.update(sysUser);
        if (sysUser.getRoleIdList() != null) {
            iSysUserRoleService.saveOrUpdate(sysUser.getUserId(), sysUser.getRoleIdList());
        }

        //保存用户与商圈的关系
        if (sysUser.getBusinessCircleIdList() != null) {
            iTbConfigUserBusinessCircleService.saveOrUpdate(sysUser.getUserId(), sysUser.getBusinessCircleIdList());
        }
    }

    @Override
    public void saveSysUser(SysUser sysUser) {

        sysUser.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), salt).toHex());
        sysUser.setSalt(salt);
        baseMapper.save(sysUser);
        if (sysUser.getRoleIdList() != null) {
            iSysUserRoleService.saveOrUpdate(sysUser.getUserId(), sysUser.getRoleIdList());
        }

        //保存用户与商圈的关系
        if (sysUser.getBusinessCircleIdList() != null) {
            iTbConfigUserBusinessCircleService.saveOrUpdate(sysUser.getUserId(), sysUser.getBusinessCircleIdList());
        }
    }

    @Override
    public List<SysUser> queryList2(Long deptId, Integer id) {
        return baseMapper.queryList2(deptId, id);
    }

    @Override
    public void updateUser2(SysUser user) {
        //tbBusiness调这里来保存用户的一些改变

//       1 改用户的审核状态  改用户的审核状态  改用户的审核状态

        baseMapper.updateUser2(user);


//            2    改用户和角色之间的关系    改用户和角色之间的关系
        iSysUserRoleService.saveOrUpdate2(user.getUserId(), user.getRoleIdList());


    }


    public SysUser getUserById(Long userId) {
        return baseMapper.getUserById(userId);
    }

    @Override
    public void register(SysUser register) {
        baseMapper.register(register);
    }
    @Override
    public void update3(SysUser sysUserEntity) {
        baseMapper.update3(sysUserEntity);
    }

    @Override
    public SysUserDto getBusinessIdByUserId(String username) {
        return baseMapper.getBusinessIdByUserId(username);
    }

}
